package me.filter;


import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebFilter(urlPatterns = {"/index.jsp","/update.jsp","/add.jsp","/studentController",
        "/indexController","/addController","/deleteController","/modifyController","/updateController"})
public class PermissionEncoding implements Filter {

    @Override
    public void init(FilterConfig filterConfig) throws ServletException {

    }

    @Override
    public void destroy() {

    }

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {

        //强制转换一下
        HttpServletRequest request = (HttpServletRequest) servletRequest;
        HttpServletResponse response = (HttpServletResponse)servletResponse;

        //获取保存的admin信息
        Object admin_permission = request.getSession().getAttribute("ADMIN_PERMISSION");
        if(admin_permission!=null){
            //如果信息不为空，那就说明已经登录了，就不会拦截
            filterChain.doFilter(servletRequest,servletResponse);
        }else{
            //否则的话，重新回到登录页面
            response.sendRedirect("login.jsp");
        }
    }
}
